import tkinter as tk



root = tk.Tk()
root.title("Tkinter 示例")
root.geometry("800x600")

lvar = tk.StringVar()
label = tk.Label(root, text="欢迎使用Tkinter！",
                 background="yellow",textvariable=lvar,font=("Arial", 12),width=30,height=5,)
label.pack(side=tk.LEFT, pady=20)

items = ["选项1", "选项2", "选项3", "选项4", "选项5"]
selected_items = []

def print_selected_items():
    selected_items.clear()
    # 获取所有选中项的索引
    selected_indices = listbox.curselection()
    for index in selected_indices:
        # 根据索引获取选中项的值
        selected_items.append(listbox.get(index))
    print("选中的项目：", selected_items)
    lvar.set(selected_items)


listbox = tk.Listbox(root, selectmode=tk.MULTIPLE,height=5,width=20)
listbox.pack(side=tk.LEFT)
for item in items:
    listbox.insert(tk.END, item)


button = tk.Button(root, text="点击我", command=print_selected_items,height=2,width=20)
button.pack(side=tk.LEFT, pady=20)



root.mainloop()